package com.situ.mymall.controller;

import com.situ.mymall.annotation.MyLog;
import com.situ.mymall.pojo.entity.Admin;
import com.situ.mymall.pojo.query.AdminQuery;
import com.situ.mymall.service.IAdminService;
import com.situ.mymall.util.CookieUtils;
import com.situ.mymall.util.PageResult;
import com.situ.mymall.util.Result;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @RequestMapping("/list")
    public PageResult list(AdminQuery adminQuery){
        return adminService.list(adminQuery);
    }

    @RequestMapping("/selectById")
    public Result selectById(Integer id){
        Admin admin = adminService.selectById(id);
        return Result.ok(admin);
    }

    @RequestMapping("/add")
    public Result add(Admin admin){
        adminService.add(admin);
        return Result.ok("添加成功");
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        adminService.deleteById(id);
        return Result.ok("删除成功");
    }

    @RequestMapping("/deleteAll")
    public Result deleteAll(Integer[] ids){
        adminService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    @RequestMapping("/update")
    public Result update(Admin admin){
        adminService.update(admin);
        return Result.ok("更新成功");
    }

    @RequestMapping("/updateStatus")
    public Result updateStatus(Integer id,Integer status){
        adminService.updateStatus(id,status);
        return Result.ok("更新成功");
    }

    @MyLog(module = "密码修改模块",desc = "返回列表")
    @RequestMapping("/updatePass")
    public Result updatePass(Integer id,String oldPassword,Integer newPassword,HttpServletRequest req){

        HttpSession session = req.getSession();
        Admin a = (Admin) session.getAttribute("admin");
        System.out.println("原密码: " + a.getPassword());
        //使用equals比较，数据类型为字符串
        if (!a.getPassword().equals(oldPassword)){
            return Result.error("原密码输入错误");
        }

        adminService.updatePass(id,newPassword);
        return Result.ok("密码修改成功");

    }

    @MyLog(module = "管理员信息修改模块",desc = "返回列表")
    @RequestMapping("/updateAdmin")
    public Result updateAdmin(Admin admin){
        adminService.updateAdmin(admin);
        return Result.ok("管理员信息修改成功");
    }

    @MyLog(module = "管理员头像修改模块",desc = "返回列表")
    @RequestMapping("/updatePic")
    public Result updatePic(Admin admin,HttpSession session){
        Admin a = (Admin) session.getAttribute("admin");
        admin.setImage(a.getImage());
        adminService.updatePic(admin);
        return Result.ok("图片上传成功");
    }



    @RequestMapping("/login")
    public Result login(String name, String password,String code,HttpSession session,HttpServletRequest req, HttpServletResponse resp){
        Admin admin = adminService.login(name,password);

        String codeInSession = (String) session.getAttribute("codeInSession");
        if (!codeInSession.equalsIgnoreCase(code)){
            return Result.error("验证码错误");
        }


        if (admin == null){
            return Result.error("用户名或密码错误");
        }

        //自动登录
        //判断有没有勾选自动登录
        String autoLogin = req.getParameter("autoLogin");
        if (!ObjectUtils.isEmpty(autoLogin)){
            Cookie cookie = new Cookie("autoLogin", admin.getName() + "&" + admin.getPassword());
            //设置Cookie的存活时间和绑定路径
            cookie.setMaxAge(60*60*24*7); //设置时间为1周
            cookie.setPath("/");
            //在响应resp中添加cookie,返回给浏览器
            resp.addCookie(cookie);
        }

        session.setAttribute("admin",admin);
        return Result.ok("登录成功");
    }

    @RequestMapping("/logout")
    public Result logout(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session = req.getSession();
        session.removeAttribute("admin");

        Cookie cookie = CookieUtils.getCookie(req, "autoLogin");
        if (cookie != null){
            cookie.setMaxAge(0);
            resp.addCookie(cookie);
        }
        return Result.ok("退出成功");
    }

}
